<?php


// +----------------------------------------------------------------------


// | Description: 解决跨域问题


// +----------------------------------------------------------------------


// | Author:


// +----------------------------------------------------------------------





namespace app\common\controller;





use app\admin\logic\AppLogic;


use app\admin\model\Access;


use think\facade\Cache;


use think\facade\Db;


use app\BaseController as BaseController;


use think\facade\Request;


use think\Model;





class Common extends BaseController


{


    public $param;


    public $m;


    public $c;


    public $a;


    public $userInfo;


    public function initialize()


    {


        parent::initialize();


        /*防止跨域*/


       // header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);


        header('Access-Control-Allow-Origin:*');


        header('Access-Control-Allow-Credentials: true');


        header('Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS');


        header('Access-Control-Allow-Headers:*');


        $param = $this->request->param();


        $this->param = $param;


        $request = request();


        $header = $request->header();


        $platform = $header['platform'] ?? '';


        if(!$platform){


            header('Content-Type:application/json; charset=utf-8');


            exit(json_encode([


                'code' => 400,


                'data' => [],


                'msg'  => '平台类型不能为空!'


            ]));


        }


        $authKey = isset($header['auth-key']) && $header['auth-key'] ? trim($header['auth-key']) : '';


        //$authKey = isset($header['authkey'])? trim($header['authkey']) : '';


        $cache = Cache::get('auth:'.$authKey);


        if ($cache) $this->userInfo = $cache['userInfo'];


        $m = \think\facade\App::initialize()->http->getName();


        $c = strtolower(Request::controller());


        $a = strtolower(Request::action());


        $this->m = $m;


        $this->c = $c;


        $this->a = $a;


        //判断app更新


        if (in_array($platform, ['ios', 'android'])) {


            $appVersion = $header['app-version'] ?? '';


            if(!$appVersion){


                header('Content-Type:application/json; charset=utf-8');


                exit(json_encode([


                    'code' => 400,


                    'data' => [],


                    'msg'  => 'app版本不能为空!'


                ]));


            }


            $appConfig = AppLogic::get($platform);


            if($appConfig){


                $appConfig['app_close_status'] = $appConfig['app_close_status'] ?? 0;


                $appConfig['app_update_status'] = $appConfig['app_update_status'] ?? 0;


                $appConfig['app_version'] = $appConfig['app_version'] ?? 0;


                if($appConfig['app_close_status']){


                    header('Content-Type:application/json; charset=utf-8');


                    exit(json_encode([


                        'code' => 401,


                        'data' => [],


                        'msg'  => 'app禁止登陆!'


                    ]));


                }


                if($appConfig['app_version']){


                    $confVersion = str_replace(".", "", $appConfig['app_version']);


                    $appVersion = str_replace(".", "", $appVersion);//接口传参


                    if ($confVersion > $appVersion) {


                        if($appConfig['app_update_status']){//强制更新


                            header('Content-Type:application/json; charset=utf-8');


                            exit(json_encode([


                                'code' => 102,


                                'data' => [


                                    'url'                   => $appConfig['app_path'] ?? '',


                                    'log'                   => $appConfig['app_log'],


                                    'app_version'           => $appConfig['app_version'],


                                    'app_update_status'     => $appConfig['app_update_status']


                                ],


                                'msg'  => '有新版本更新!'


                            ]));


                        }


                    }


                }


            }


        }


    }


    /**


     * 根据用户类型获取店铺ID集合  系统用户 根绝角色获取店铺ID  店铺账户员工 自身店铺


     * @author ran


     * @date 2021-11-17 14:44


     * mailbox 466180170@qq.com


     */


    public function getCommonShopIdsCollectByUserType():array


    {

        switch ($this->userInfo['type']) {

            
            case 1:

                $accessModel = new Access();


                $dataAuth =$accessModel->getCommonUserDataAuth($this->userInfo['id']);


                $shopIds =$dataAuth['market']??[];


                break;


            default:

                $relationShop =Db::name('shop')->where('shop_id',$this->userInfo['shop_id'])->value('relation_shop');


                if($relationShop != "null" && !empty($relationShop)){


                    $relationShop =json_decode($relationShop,true);


                    $shopIds =array_merge($relationShop,[$this->userInfo['shop_id']]);


                }else{


                    $shopIds=[$this->userInfo['shop_id']];


                }


                $shopIds =$this->userInfo['shop_id']?$shopIds:[];


                break;


        }


        return $shopIds;


    }





    /**


     * @param $array


     * @return array|mixed


     * @author ran


     * @date 2021-11-17 15:13


     * mailbox 466180170@qq.com


     */


    public function object_array($array)


    {


        if (is_object($array)) {


            $array = (array)$array;


        }


        if (is_array($array)) {


            foreach ($array as $key=>$value) {


                $array[$key] = $this->object_array($value);


            }


        }


        return $array;


    }


}


